﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.DALFactory;

    public class AdjustmentDetailBLL
    {
        private static readonly IAdjustmentDetailDA adjustmentdetailDA = DataAccess.CreateAdjustmentDetailDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;

        public AdjustmentDetailBLL()
        {
            this.pubParasInfo = null;
        }

        public AdjustmentDetailBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public AdjustmentDetailBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteAdJustMentDetail(string IDS, List<string> LineID)
        {
            string temp = string.Empty;
            try
            {
                List<AdjustmentDetailInfo> adjustmentDetailInfos = new List<AdjustmentDetailInfo>();
                AdjustmentDetailInfo adjustmentDetailInfo = null;
                foreach (string id in LineID)
                {
                    adjustmentDetailInfo = adjustmentdetailDA.GetAdjustmentDetailByID(id, this.dataBaseID[0]);
                    adjustmentDetailInfos.Add(adjustmentDetailInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (adjustmentdetailDA.DeleteAdjustmentDetail(IDS, LineID, this.dataBaseID[0]) > 0)
                {
                    for (int i = 0; i < LineID.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<AdjustmentDetailInfo>(adjustmentDetailInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "ADJUSTMENT_DETAIL", this.dataBaseID[0]);
                        temp = temp + LineID[i] + ",";
                    }
                    return temp.Substring(0, temp.Length - 1);
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return temp;
        }

        public AdjustmentDetailInfo GetAdJustMentDetailInfoByID(string id)
        {
            return adjustmentdetailDA.GetAdjustmentDetailByID(id, this.dataBaseID[0]);
        }

        public string InsertAdJustMentDetail(AdjustmentDetailInfo obj)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string AdJustMent = string.Empty;
            try
            {
                obj.UpdatedBy = obj.CreatedBy;
                obj.CreatedDate = DateTime.Now;
                obj.UpdatedDate = DateTime.Now;
                if (adjustmentdetailDA.InsertAdjustmentDetail(dataBase, tran, obj, this.dataBaseID[0]) > 0)
                {
                    new AuditDataBLL().InsertAuditData<AdjustmentDetailInfo>(dataBase, tran, null, obj, AuditAction.Insert, this.pubParasInfo.UserID, "ADJUSTMENT_DETAIL", this.dataBaseID[0]);
                    AdJustMent = obj.AdjustmentID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return AdJustMent;
        }

        public string UpdateAdJustMentDetail(AdjustmentDetailInfo obj)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string AdJustMentID = string.Empty;
            try
            {
                AdjustmentDetailInfo oldAdjustmentDetailInfo = adjustmentdetailDA.GetAdjustmentDetailByID(dataBase, tran, obj.AdjustmentID, this.dataBaseID[0]);
                obj.UpdatedBy = this.pubParasInfo.UserID;
                obj.UpdatedDate = DateTime.Now;
                if (adjustmentdetailDA.UpdateAdjustmentDetail(dataBase, tran, obj, this.dataBaseID[0]) > 0)
                {
                    new AuditDataBLL().InsertAuditData<AdjustmentDetailInfo>(dataBase, tran, oldAdjustmentDetailInfo, obj, AuditAction.Update, this.pubParasInfo.UserID, "ADJUSTMENT_DETAIL", this.dataBaseID[0]);
                    AdJustMentID = obj.AdjustmentID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return "";
        }
    }
}

